/**
 * Created by 23827 on 2019/10/9.
 */
$(function(){
    //隐藏警告信息
    $(".alert-warning").hide();

    $("#commit").click(function(){
        $.ajax({
            //提交数据的类型 POST GET
            type:"POST",
            //提交的网址
            url:"/address/add",
            //提交的数据
            data: JSON.stringify(GetJsonData()),
            //参数格式为json
            contentType: "application/json; charset=utf-8",
            //返回数据的格式
            datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
            //成功返回之后调用的函数
            success:function(data){
                console.log(data);
                if("OK" == data.codeStatus){
                    window.location="/address/list"
                }else {
                    alert("失败")
                }
            },
            //调用出错执行的函数
            error: function(){
                //请求出错处理
                console.log(JSON.stringify(GetJsonData()));
                alert("请求失败");
            }
        });
    });
    //json格式的数据源
    function GetJsonData() {
        var userId = $("#userId").text();
        var username = $("#username").val();
        var phoneNumber = $("#phoneNumber").val();
        var provinceName = $("#province1").find("option:selected").text();
        var cityName = $("#city1").find("option:selected").text();
        var areaName = $("#district1").find("option:selected").text();
        var addressDeatil = $("#addressDeatil").val();
        var json = {
            "userId":userId,
            "username":username,
            "phoneNumber": phoneNumber,
            "provinceName": provinceName,
            "cityName": cityName,
            "areaName": areaName,
            "addressDeatil": addressDeatil,
            "dataFlag":1,
            "isDefault":0

        };
        return json;
    }
});


